####################################################################
#***2019 analysis
## Packages
# To install and open the R packages that you need for this code. 
need <- c('tidyverse','readstata13','lfe','glue','rdrobust', 'stargazer','arm', 'broom', 'ggplot2', 'dotwhisker', 'gridExtra')
have <- need %in% rownames(installed.packages()) 
if(any(!have)) install.packages(need[!have]) 
invisible(lapply(need, library, character.only=T)) 

# Change path to whereever you place the models
# To set up the working directory. 
script_folder = dirname(rstudioapi::getSourceEditorContext()$path)
setwd(glue('{script_folder}'))
rm(list = ls())
setwd("../")

## load in RD data:
load("5prepdata/final_rd_data.RData")

##################################################################
# Main effects
##################################################################

# interact won x delegation size/number of seats in congress
rd.data <- rd.data %>% ungroup %>% 
  group_by(state.x) %>% 
  mutate(state.del.size = max(district.x, na.rm=T))
rd.data$del.size.prop <- rd.data$state.del.size/rd.data$del.size
rd.data$del.size.propXwon <- rd.data$del.size.prop * rd.data$won_election

main.specification <- function(x) {
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  
  out <- felm(x[rd.data$kw>0] ~ won_election + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  bw <- round(rd.data$bandwidth[1], 2)
  
  return(list(out, bw))
}



main.specification.manualbw <- function(x,manual.bandwidth) {
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  rd.data$bandwidth<-manual.bandwidth

  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  
  out <- felm(x[rd.data$kw>0] ~ won_election + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  bw <- manual.bandwidth
  
  return(list(out, bw))
}



extract.mean <- function(x) {
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  rd.data$cutoff <- 0
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  out <- round(mean(x[rd.data$kw>0]), 3)
  return(out)
}

extract.sd <- function(x) {
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  rd.data$cutoff <- 0
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  out <- round(sd(x[rd.data$kw>0]), 3)
  return(out)
}


#################################################################
# HOUSE
#################################################################
m1<-main.specification.manualbw(rd.data$ever_ranprim_h, .05)
m2<-main.specification.manualbw(rd.data$ever_winprim_h, .05)
m3<-main.specification.manualbw(rd.data$ever_runhouse, .05)
m4<-main.specification.manualbw(rd.data$ever_winhouse, .05)

# THIS WHOLE SECTION OF CODE GENERATES APPENDIX TABLE 1

b05<-stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service on career progression to Congressional candidacy and representation",
          font.size =  "scriptsize",
          style = "apsr",
          model.numbers=T,
          label = "tab:bandwidths",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          keep = c("won_election"),
          covariate.labels = c("Won state legis. seat"),
          notes = "\\parbox[t]{\\linewidth}{This table reports estimates of the effect of an additional state legislative term on individuals' career progression to Congress. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}" # *** = significant at 1 percent level, ** = significant at 5 percent level, * = significant at 10 percent level.
)



m1<-main.specification.manualbw(rd.data$ever_ranprim_h, .1)
m2<-main.specification.manualbw(rd.data$ever_winprim_h, .1)
m3<-main.specification.manualbw(rd.data$ever_runhouse, .1)
m4<-main.specification.manualbw(rd.data$ever_winhouse, .1)



b10 <- stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service on career progression to Congressional candidacy and representation",
          font.size =  "scriptsize",
          style = "apsr",
          model.numbers=T,
          label = "tab:bandwidths",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          keep = c("won_election"),
          covariate.labels = c("Won state legis. seat"),
          notes = "\\parbox[t]{\\linewidth}{This table reports estimates of the effect of an additional state legislative term on individuals' career progression to Congress. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}" # *** = significant at 1 percent level, ** = significant at 5 percent level, * = significant at 10 percent level.
)


m1<-main.specification.manualbw(rd.data$ever_ranprim_h, .2)
m2<-main.specification.manualbw(rd.data$ever_winprim_h, .2)
m3<-main.specification.manualbw(rd.data$ever_runhouse, .2)
m4<-main.specification.manualbw(rd.data$ever_winhouse, .2)


b20 <- stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service on career progression to Congressional candidacy and representation",
          font.size =  "scriptsize",
          style = "apsr",
          model.numbers=T,
          label = "tab:bandwidths",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          keep = c("won_election"),
          covariate.labels = c("Won state legis. seat"),
          notes = "\\parbox[t]{\\linewidth}{This table reports estimates of the effect of an additional state legislative term on individuals' career progression to Congress. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}" # *** = significant at 1 percent level, ** = significant at 5 percent level, * = significant at 10 percent level.
)

m1<-main.specification.manualbw(rd.data$ever_ranprim_h, .3)
m2<-main.specification.manualbw(rd.data$ever_winprim_h, .3)
m3<-main.specification.manualbw(rd.data$ever_runhouse, .3)
m4<-main.specification.manualbw(rd.data$ever_winhouse, .3)


b30 <- stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service on career progression to Congressional candidacy and representation",
          font.size =  "scriptsize",
          style = "apsr",
          model.numbers=T,
          label = "tab:bandwidths",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          keep = c("won_election"),
          covariate.labels = c("Won state legis. seat"),
          notes = "\\parbox[t]{\\linewidth}{This table reports estimates of the effect of an additional state legislative term on individuals' career progression to Congress. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}" # *** = significant at 1 percent level, ** = significant at 5 percent level, * = significant at 10 percent level.
)

source("3analysis/starpanelSOC.R")

setwd('7tex/manuscript/tables/sourcefiles/')

tableList <- c(b05, b10, b20, b30)
panelNames <- c('Bandwidth: 0.05', 'Bandwidth: 0.10', 'Bandwidth: 0.20', 'Bandwidth: 0.30')


panelOut <- star_panel(b05, b10, b20, b30, 
                       panel.names = panelNames, 
                       extra.rows = 0)
panelOut <- panelOut[!is.na(panelOut)]
panelOut
star_tex_write(panelOut, file = "Appendix Table 1.tex", headers = FALSE)


